package org.dna.metronomo.persistence.impl;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.tigris.scarab.model.ActivityTypeEnum;

/**
 * MyBatis TypeHandler for the ActivityTypeEnum, convert String <-> ActivityTypeEnum.
 *
 * @author andrea
 */
public class ActivityTypeEnumTypeHandler extends BaseTypeHandler {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, ((ActivityTypeEnum)parameter).value());
    }

    @Override
    public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return ActivityTypeEnum.valueOf(rs.getString(columnName));
    }

    @Override
    public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return ActivityTypeEnum.valueOf(cs.getString(columnIndex));
    }

}
